package net.sf.midollo.net.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.StreamConnection;

import net.sf.midollo.io.IOStream;

public class StreamConnectionIOStream implements IOStream {

  protected StreamConnection sc;
  protected DataInputStream is;
  protected DataOutputStream os;

  public StreamConnectionIOStream(StreamConnection sc) throws IOException {
    this.sc = sc;
  }

  public void close() throws IOException {
    try {
      if (this.os != null)
        this.os.close();
    } finally {
      try {
        if (this.is != null)
          this.is.close();
      } finally {
        if (this.sc != null)
          this.sc.close();
      }
    }
  }

  public InputStream getInput() throws IOException {
    if (this.is == null)
      this.is = this.sc.openDataInputStream();
    return this.is;
  }

  public OutputStream getOutput() throws IOException {
    if (this.os == null)
      this.os = this.sc.openDataOutputStream();
    return this.os;
  }
}
